﻿using Hydrogen.Cmd.Interfaces;
using Hydrogen.Console;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SConsole = System.Console;

namespace Hydrogen.Cmd.buildinCmds
{
    /// <summary>
    /// 列出所有命令
    /// </summary>
    [CommandFlag("lc", "listcommand")]
    class ListCommandCommand : CommandBase
    {
        public static new string Discription
        {
            get
            {
                string description = "Inspect All Command.";
                return description;
            }
        }
        public override void Invoke(params string[] args)
        {
            Dictionary<Type, List<string>> cmds = new Dictionary<Type, List<string>>();
            foreach (var item in Program.commDic)
            {
                if (cmds.ContainsKey(item.Value)) cmds[item.Value].Add(item.Key);
                else
                {
                    cmds.Add(item.Value, new List<string>() { item.Key });
                }
            }

            //SConsole.Write("命令列表：");
            //Console.ConsoleLog.WriteColor("可选命令符 ", ConsoleColor.Cyan);
            //Console.ConsoleLog.WriteColorLine("描述", ConsoleColor.Green);
            //int count = cmds.Count;
            //int index = -1;
            //foreach (var item in cmds)
            //{
            //    index++;
            //    if (index < count - 1) SConsole.Write("\t┣  ");
            //    else SConsole.Write("\t┗  ");
            //    SConsole.ForegroundColor = ConsoleColor.Cyan;
            //    foreach (var flag in item.Value)
            //    {
            //        SConsole.Write("{0} ", flag);
            //    }
            //    SConsole.ForegroundColor = ConsoleColor.Green;
            //    var description = item.Key.GetProperty(nameof(CommandBase.Discription))?.GetValue(null);
            //    SConsole.WriteLine(description);
            //    SConsole.ResetColor();
            //}
            List<ConsoleTreeNode> nodeList = new List<ConsoleTreeNode>();
            nodeList.Add(new ConsoleTreeNode()
            {
                level = 0,
                onDraw = () =>
                {
                    SConsole.Write("命令列表：");
                    ConsoleLog.WriteColor("可选命令符 ", ConsoleColor.Cyan);
                    ConsoleLog.WriteColorLine("描述", ConsoleColor.Green);
                }
            });

            foreach (var item in cmds)
            {
                nodeList.Add(new ConsoleTreeNode()
                {
                    level = 1,
                    onDraw = () =>
                    {
                        string cmdStr = "";
                        foreach (var flag in item.Value)
                        {
                            //ConsoleLog.WriteColor("{0} ", ConsoleColor.Cyan, flag);
                            cmdStr += (flag + " ");
                        }
                        ConsoleLog.WriteColor("{0} ", ConsoleColor.Cyan, cmdStr);
                        SConsole.WriteLine();
                    }
                });
                var description = item.Key.GetProperty(nameof(CommandBase.Discription))?.GetValue(null);
                if (description != null)
                {
                    nodeList.Add(new ConsoleTreeNode()
                    {
                        level = 2,
                        onDraw = () =>
                        {
                            ConsoleLog.WriteColorLine("[Description] {0}", ConsoleColor.Green, description != null ? description.ToString() : "");
                        }
                    });
                }
            }

            ConsoleLog.Write(nodeList);

        }
    }
}
